import javax.print.DocFlavor;
import java.util.*;

public class Test {

    public List<String> topKFrequent(String[] words, int k) {

        //1.统计每一个单词出现的次数
        Map<String,Integer> map = new HashMap<>();
        for(String word:words){
            if (map.get(word)==null){
                map.put(word,1);
            }else {
                int val = map.get(word);
                map.put(word,val+1);
            }
        }

        //2.建立小根堆,指定比较的方式
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
               if (o1.getValue().compareTo(o2.getValue())==0){
                   return o2.getKey().compareTo(o1.getKey());
               }

                return o1.getValue()-o2.getValue();
            }
        });

        //3.遍历Map 调整优先级队列
        for (Map.Entry<String,Integer> entry:map.entrySet()){
            if (minHeap.size()<k){
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                //评率相同时，根据字典序比较
                if (top.getValue().equals(entry.getValue())){
                    //字母顺序小的出来
                    if (top.getKey().compareTo(entry.getKey())>0){
                        //出队
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }else {
                    if (top.getValue().compareTo(entry.getValue())<0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }

            }
        }

        List<String> ret = new ArrayList<>();
        for (int i=0;i<k;i++){
            Map.Entry<String,Integer>  top = minHeap.poll();
            ret.add(top.getKey());
        }

        Collections.reverse(ret);

        return ret;

    }

    public static void main(String[] args) {

    }



    public static void func7(String[] words){
        Map<String,Integer> map = new HashMap<>();
        for(String word:words){
            if (map.get(word)==null){
                map.put(word,1);
            }else {
                int val = map.get(word);
                map.put(word,val+1);
            }
        }
        System.out.println(map);
    }


    public static void main7(String[] args) {
        String[] words = {"this","man","woman","happy","day","happy"};
        func7(words);
    }











    public static void main6(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            func1(s1,s2);


        }
    }

    public static void func1(String str1,String strAct){
        Set<Character> set1 = new HashSet<>();
        for(char ch:strAct.toUpperCase().toCharArray()){
            set1.add(ch);
        }
        Set<Character> set2 = new HashSet<>();

        for(char ch:str1.toUpperCase().toCharArray()){
            if (!set1.contains(ch) && !set2.contains(ch)){
                System.out.print(ch);
                set2.add(ch);
            }
        }

    }



    public static void main5(String[] args) {
        Person person1 = new Person("123");
        Person person2 = new Person("123");
        HashBuck2<Person,String> hashBuck1 = new HashBuck2<>();
        hashBuck1.put(person1,"张三");
        System.out.println(hashBuck1.get(person1));


    }


    public static void main4(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,13);
        hashBuck.put(6,16);
        hashBuck.put(5,15);
        hashBuck.put(8,17);
        hashBuck.put(45,51);
        hashBuck.put(12,41);
        hashBuck.put(4,14);
        hashBuck.put(18,14);
        System.out.println(hashBuck.get(4));



    }


    public static void main3(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("hello");
        set.add("o");
        set.add("lo");

        System.out.println(set);
    }


    public static void main2(String[] args) {
        Map<String, Integer> map = new TreeMap<>();
        TreeMap<String,Integer> map2 = new TreeMap<>();

        map2.put("this",3);
        map2.put("phe",5);
        map2.put("a",7);
        int val = map2.getOrDefault("ad",1999);
        System.out.println(val);

        Set<String> set = map2.keySet();
        System.out.println("====");

        Set<Map.Entry<String,Integer>> entries = map2.entrySet();

    }





    public static void main1(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(3);
        binarySearchTree.insert(6);
        binarySearchTree.insert(8);
        binarySearchTree.insert(5);
        System.out.println("===");

    }
}
